//日期格式化函数
function dateFormat(timestamp, format) {
	// formats格式
	// 1. Y-M-D
	// 2. Y-M-D h:m:s
	// 3. Y年M月D日
	// 4. Y年M月D日 h时m分
	format = format || 'Y-M-D';

	console.log(timestamp);
	let zero = function(value) {
		if (value < 10) {
			return "0" + value;
		}
		return value;
	}

	let myDate = timestamp ? new Date(timestamp) : new Date();

	console.log(myDate);
	let year = myDate.getFullYear();
	let month = zero(myDate.getMonth() + 1);
	let day = zero(myDate.getDate());

	let hour = zero(myDate.getHours());
	let minite = zero(myDate.getMinutes());
	let second = zero(myDate.getSeconds());

	return format.replace(/Y|M|D|h|m|s/g, function(matches) {
		return ({
			Y: year,
			M: month,
			D: day,
			h: hour,
			m: minite,
			s: second
		})[matches]
	});
}

//检查登录
function checkLogin() {
	let token = uni.getStorageSync('token')
	if (!token) {
		return true
	} else {
		return false
	}
}
//检查电话
function checkPhone(value) {
	var myreg = /^[0-9|\+]*$/
	if (!myreg.test(value)) {
		return false;
	} else {
		return true;
	}
}

function checkCNPhone(value) {
	var myreg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
	if (!myreg.test(value)) {
		return false;
	} else {
		return true;
	}
}
//获取当前月天数
function getCurrentMonthDay(year, month) {
	var flag = year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)
	var startDay = '01'
	var endDay = null
	switch (month) {
		case '01':
		case '03':
		case '05':
		case '07':
		case '08':
		case '10':
		case '12':
			endDay = 31
			break;
		case '04':
		case '06':
		case '09':
		case '11':
			endDay = 30
			break;
		case '02':
			endDay = flag ? 29 : 28
			break;
		default:
			endDay = '月份格式不正确，请重新输入！'
	}
	return {
		startTime: year + '-' + month + '-' + startDay,
		endTime: year + '-' + month + '-' + endDay
	}
}

function getDeviceUUID() {
	let deviceId = uni.getStorageSync('uni_deviceId') ||
		uni.getSystemInfoSync().deviceId ||
		uni.getSystemInfoSync().system + '_' + Math.random().toString(36).substr(2);

	uni.setStorageSync('uni_deviceId', deviceId)
	return deviceId;
}

module.exports = {
	checkLogin: checkLogin,
	dateFormat: dateFormat,
	checkPhone: checkPhone,
	checkCNPhone: checkCNPhone,
	getCurrentMonthDay: getCurrentMonthDay,
	getDeviceUUID: getDeviceUUID
}